<?php 

if (! defined('BASEPATH')) exit('No direct script access');

class branch_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }
    
    function get_by_id($branch_id = FALSE)
    {
        if ($branch_id === FALSE)
        {
            return FALSE;
        }
        
        $query = $this->db->where('id', $branch_id)
                          ->get('branch');
        if ($query->num_rows() == 1)
        {
            return $query->row();
        }
        else
        {
            return FALSE;
        }
    }
    
    
    function get_all($num = FALSE, $offset = FALSE) 
    {
        $query = $this->db->select('*' )
                          ->get('branch', $num, $offset);
        return $query->result();
    }
    
    
    function get_next_match ($num = FALSE, $offset = FALSE) 
    {
        $query = $this->db->select('id,(SELECT name FROM rounds WHERE id = fixtures.rounds_id) round_name, (SELECT en_name FROM teams WHERE id = fixtures.home_teams_id) home_team_name, (SELECT en_name FROM teams WHERE id = fixtures.home_teams_id) home_team_name_en, home_score, away_score, (SELECT en_name FROM teams WHERE id= fixtures.away_teams_id) away_team_name, (SELECT en_name FROM teams WHERE id= fixtures.away_teams_id) away_team_name_en, kick_off_time, finished, extended, penalty , (SELECT country_code FROM teams WHERE id = fixtures.home_teams_id) home_code_name , (SELECT country_code FROM teams WHERE id = fixtures.away_teams_id) away_code_name , (SELECT iso_name FROM teams WHERE id = fixtures.home_teams_id) home_iso_name , (SELECT iso_name FROM teams WHERE id = fixtures.away_teams_id) away_iso_name')
                          ->where('NOW() < kick_off_time ')
                          ->get('fixtures', $num, $offset);
        return $query->result();
    }
    
    function create($name = FALSE, $description = FALSE, $address = FALSE)
    {
        if ($name === FALSE OR $description === FALSE OR $address === FALSE )
        {
            return FALSE;
        }

        $branch = array('name' => $name,
                         'description' => $description,
                         'address' => $address
                        );
        $this->db->insert('branch', $branch);
	    return TRUE;
        
    }
    
    function update($branch_id = FAlSE , $name = FALSE, $description = FALSE, $address = FALSE)
    {
        if ($branch_id === FALSE OR $name === FALSE OR $description === FALSE OR $address === FALSE )
        {
            return FALSE;
        }
        $branch = array('name' => $name,
                         'description' => $description,
                         'address' => $address
                        );
        $this->db->where('id', $branch_id);
        $this->db->update('branch', $branch);
        return TRUE;
    }
    
    function delete($branch_id = FALSE)
    {
        if ($branch_id === FALSE)
        {
            return FALSE;
        }
        $this->db->where('id', $branch_id);
        $this->db->delete('branch');
        return TRUE;
    }
    
    function update_status($fixtures_id = FALSE, $status = FALSE)
    {
        if ($fixtures_id === FALSE OR $status === FALSE)
        {
            return FALSE;
        }
        $fixture = array('status' => $status);
        $this->db->where('id', $fixtures_id);
        $this->db->update('fixtures', $fixture);
        return TRUE;
    }
}
/* End of file fixture_model.php */
/* Location: ./system/application/models/fixture_model.php */